package pl.ptemich.nov2k13.engine;

import pl.ptemich.nov2k13.engine.HexBoard.HexType;


/**
 * Vertexes positions
 * 
 * 
 *     0 - 1
 *    /     \
 *    5     2
 *    \     /
 *     4 - 3  
 *             
 * 
 * @author Brutusek
 *
 */
public class FlatOrientationHex extends AbstractHex {

  public FlatOrientationHex(int side, float posX, float posY, int hexX, int hexY, float h, float r) {
    init(side, posX, posY, hexX, hexY, h, r, HexType.FLAT_ORIENTATION);

    vertexes = new SimplePoint[6];
    vertexes[0] = new SimplePoint(posX - (side / 2), posY - r);
    vertexes[1] = new SimplePoint(posX + (side / 2), posY - r);
    vertexes[2] = new SimplePoint(posX + (side / 2) + h, posY);
    vertexes[3] = new SimplePoint(posX + (side / 2), posY + r);
    vertexes[4] = new SimplePoint(posX - (side / 2), posY + r);
    vertexes[5] = new SimplePoint(posX - (side / 2) - h, posY);
    
    // a = (y1 - y2) / (x1 + x2)
    a1Factor = (vertexes[5].y - vertexes[0].y) / (vertexes[5].x - vertexes[0].x);
    a2Factor = (vertexes[1].y - vertexes[2].y) / (vertexes[1].x - vertexes[2].x);

    // b = y2 - a * x2
    b1tFactor = vertexes[0].y - a1Factor * vertexes[0].x;    
    b2tFactor = vertexes[2].y - a2Factor * vertexes[2].x;
    b1bFactor = vertexes[3].y - a1Factor * vertexes[3].x;
    b2bFactor = vertexes[5].y - a2Factor * vertexes[5].x;
  }

  @Override
  public boolean checkSelection(int mouseX, int mouseY) {
    if (mouseY > (center.y - r) && mouseY < (center.y + r)) {
      // reversed screen coordinates !!! Y rises to the bottom
      if (mouseY - 1 > a1Factor * mouseX + b1tFactor) { // top left
        if (mouseY + 1 < a1Factor * mouseX + b1bFactor) { // bottom right
          if (mouseY - 1 > a2Factor * mouseX + b2tFactor) { // top right
            if (mouseY + 1 < a2Factor * mouseX + b2bFactor) { // bottom left
              return true;
            }
          }
        }
      }
    }
    return false;
  }

  
}
